package database;

import java.util.Enumeration;
import java.util.Hashtable;

import dbmanager.DBManager;

public class RecordUpdate 
{
	private String table = "property_details";
	DBManager database;
	private Hashtable<Object, Object> oldHash;
	private Hashtable<Object, Object> newHash;
	private String query = null;
	private String newHashString = "";
	private String oldHashString = "";
	
	public void setDatabase(DBManager database)
    {
    	this.database = database;
    }
    
	
	public void setTableName(String tableName) 
    {
    	this.table = tableName;	
	}
    
	  
    /**
     * setConditionHash() method will sets the condition for the table query part.
     * */
    public void setOldCondition(Hashtable<Object, Object> condition)
	{
		this.oldHash = new Hashtable<Object, Object>();
		this.oldHash.putAll(condition);
		oldHashQuery(); // Calls the method for converting into the String.
	}
    
    /**
     * setTableHash() method will sets the table hash for the table query part.
     * */
    public void setNewCondition(Hashtable<Object, Object> condition)
	{
		this.newHash = new Hashtable<Object, Object>();
		this.newHash.putAll(condition);
		newHashQuery(); // Calls the method for converting into the String.
	}

    /**
     * oldHashQuery() - It will creats the string from the Hash Table. Which get stores into the respected string variable.  
     * */
    private void oldHashQuery()
    {
     	if(oldHash.isEmpty())
		{
			System.out.println(" Old Value Is Not Present ");
			//query = "SELECT * FROM " + table;
		}
		else
		{
			Enumeration<Object> hashKeys;
			hashKeys = oldHash.keys();
			
			while(hashKeys.hasMoreElements())
			{
				String columnmName = hashKeys.nextElement().toString();
				String columnValue = oldHash.get(columnmName).toString();
				oldHashString = oldHashString  + columnmName + " = '" + columnValue + "' AND "; 
			}
			oldHashString  = oldHashString .substring(0, oldHashString .length() - 4);
			//query = "SELECT mid, pid, pv, vt, ref, fd, td FROM " + table + " where " + tableHashQuery;
		}
    }
  
    /**
     * newHashQuery() - It will creats the string from the Hash Table. Which get stores into the respected string variable.  
     * */
    private void newHashQuery() 
    {
    	if(newHash.isEmpty())
		{
			System.out.println(" Condition Value Is Not Present ");
			//query = "UPDATE " + table + ;
		}
		else
		{
			Enumeration<Object> hashKeys;
			hashKeys = newHash.keys();
			
			while(hashKeys.hasMoreElements())
			{
				String columnmName = hashKeys.nextElement().toString();
				String columnValue = newHash.get(columnmName).toString();
				newHashString = newHashString + columnmName + " = '" + columnValue + "', "; 
			}
			newHashString = newHashString.substring(0, newHashString.length() - 2);
			//query = "SELECT mid, pid, pv, vt, ref, fd, td FROM " + table + " where " + conditionHashQuery;
		}
	}
    
    public synchronized int getUpdate()
    {
  	  	try
  	  	{
  	  		  query = "UPDATE " + table;
  	  		  
  	  		  try 
  	  		  {
				if(newHashString.length() != 0)
					  query = query + " SET " + newHashString;
			  } 
  	  		  catch (Exception e)  { }
  	  		  
  	  		  try 
  	  		  {
				if(oldHashString.length() != 0)
					  query = query + " WHERE " + oldHashString;
			  } 
  	  		  catch (Exception e) { }
  	  		int j=database.getUpdate(query);
  	  		if(j>0)
    		{
    			 System.out.println("Data Updated successfully");
    		}
    		return j;
    	}
	  	catch(Exception e)
	  	{
	  		  e.printStackTrace();
	  	}
	  	return 0;
    }
    
    public static void main(String[] args) 
    {
    	Hashtable<Object, Object> old = new Hashtable<Object, Object>();
    	/*old.put("mid", "9378");
		old.put("pid", "25");*/
					
		Hashtable<Object, Object> newHash = new Hashtable<Object, Object>();
		/*newHash.put("mid", "9378");
		newHash.put("pid", "25");
		newHash.put("vt", "knowledge");*/
		
		DBManager database = new DBManager();
		database.getConnect();
		RecordUpdate ru  = new RecordUpdate();
		ru.setDatabase(database);
		ru.setOldCondition(old);
		ru.setNewCondition(newHash);
		ru.getUpdate();
		database.closeConnection();
	}
}
